<script type="text/javascript">
$(document).ready(function() {
    //datepicker
	$('[datepicker="datepicker"]').each(function(){
		$(this).datepicker({
			  dateFormat: 'yy-mm-dd'
			, minDate: new Date(new Date().getTime() )
			, onClose: function(dateText, inst) {
				$('[name="'+$(this).attr('target') + '_year"]').val(inst.selectedYear)
				$('[name="'+$(this).attr('target') + '_month"]').val(inst.selectedMonth + 1)
				$('[name="'+$(this).attr('target') + '_day"]').val(inst.selectedDay)
			}
		}).attr("readonly", true)
    });

    //maker
	$("#makerInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'distributionAjax', action: 'staffList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#maker\\.id").val(ui.item.id);
        }
    });

    //driver
	$("#driverInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'distributionAjax', action: 'staffList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#driver\\.id").val(ui.item.id);
        }
    });

    //retailer
	$("#retailerInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'distributionAjax', action: 'retailerList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#retailer\\.id").val(ui.item.id);
        }
    });
    //package
	$('[name$="packagesInput"]').each(function(){
        var name = this.name
        $(this).autocomplete({
            minLength: 0,
            source: function(request, response) {
               $.ajax({
                    url: "${createLink(controller:'distributionAjax', action: 'packageList')}",
                    data: { term: request.term },
                    dataType: "json",
                    type: "POST",
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                            return{
                                id : item[0],
                                value: item[1]+'-'+item[2],
                                label: item[1]+'-'+item[2]
                            }
                        }));
                    }
                });
            },
            select: function( event, ui ) {
                $(this).next().val(ui.item.id);
            }
        });
    });
});
</script>
